//
//  iCloudTestViewController.m
//  icloudtest
//
//  Created by iMac-hxsd on 12-2-18.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "iCloudTestViewController.h"

@implementation iCloudTestViewController

// 文件名称
#define DATA_FILE_NAME @"iclouddata.txt"

@synthesize filePath;
@synthesize icloudURL;
@synthesize textView;
@synthesize label;

- (id) initWithNibName : (NSString *) nibNameOrNil bundle :(NSBundle *) nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}

- (void) didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void) viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    // 得到文件管理器
    NSFileManager *manager = [NSFileManager defaultManager];

    // 验证iCloud是否激活
    NSURL *url = [manager URLForUbiquityContainerIdentifier:nil];
    if (url == nil)
    {
        NSLog(@"iCloud未激活");
        return;
    }

    // 指定iCloud完整路径
    self.icloudURL = [NSURL URLWithString:DATA_FILE_NAME relativeToURL:url];
    NSLog(@"self.icloudURL = %@", self.icloudURL);

    // 得到本程序沙盒路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    self.filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:DATA_FILE_NAME];
    NSLog(@"self.filePath = %@", self.filePath);
}

- (void) viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    self.filePath = nil;
    self.icloudURL = nil;
    self.textView = nil;
    self.label = nil;
}

- (BOOL) shouldAutorotateToInterfaceOrientation : (UIInterfaceOrientation) interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - Actions

// 按下上传按钮
- (IBAction) uploadPressed :(id) sender
{
    // 另起一个线程上传
    [NSThread detachNewThreadSelector:@selector(uploadICloud) toTarget:self withObject:nil];
}

// 上传到 iCloud
- (void) uploadICloud
{
    // 调用主进程的方法更新界面,在主进程外更新界面常会引起错误
    [self performSelectorOnMainThread:@selector(setString:) withObject:@"开始上传" waitUntilDone:NO];

    NSFileManager *manager = [NSFileManager defaultManager];

    NSArray *data = [[NSArray alloc] initWithObjects:self.textView.text, nil];

    // 判断本地文件是否存在
    if (![manager fileExistsAtPath:self.filePath])
    {
        // 不存在则创建
        if (![data writeToFile:self.filePath atomically:YES])
        {
            [self performSelectorOnMainThread:@selector(setString:)
            withObject:@"写本地文件失败"
            waitUntilDone:NO];
        }
    }

    // 判断iCloud里该文件是否存在
    if ([manager isUbiquitousItemAtURL:self.icloudURL])
    {
        // 存在则修改
        if (![data writeToURL:self.icloudURL atomically:YES])
        {
            [self performSelectorOnMainThread:@selector(setString:)
            withObject:@"写iCloud文件失败"
            waitUntilDone:NO];
        }
        [self performSelectorOnMainThread:@selector(setString:)
        withObject:@"上传成功"
        waitUntilDone:NO];
        return;
    }

    // 上传至iCloud
    // 指定本地文件完整路径
    NSURL *url = [NSURL fileURLWithPath:self.filePath];
    NSError *error;
    // 官方文档建议本方法不要在主进程里执行
    if (![manager setUbiquitous:YES itemAtURL:url destinationURL:self.icloudURL error:&error])
    {
        self.label.text = @"上传失败";
        return;
    }
    self.label.text = @"上传成功";
}

// 按下下载按钮
- (IBAction) downloadPressed: (id) sender
{
    self.label.text = @"开始下载";

    // 下载icloud文件
    if (![self downloadFileIfNotAvailable:self.icloudURL])
    {
        self.label.text = @"下载失败";
        return;
    }
    // 更新界面
    NSArray *array = [[NSArray alloc] initWithContentsOfURL:self.icloudURL];
    self.textView.text = [array objectAtIndex:0];

    self.label.text = @"下载成功";
}


#pragma mark - Methods

// 检查文件状态并下载
- (BOOL) downloadFileIfNotAvailable : (NSURL*)file
{
    NSNumber*  isIniCloud = nil;

    if ([file getResourceValue:&isIniCloud forKey:NSURLIsUbiquitousItemKey error:nil])
    {
        // If the item is in iCloud, see if it is downloaded.
        if ([isIniCloud boolValue])
        {
            NSNumber *isDownloaded = nil;
            if ([file getResourceValue:&isDownloaded forKey:NSURLUbiquitousItemIsDownloadedKey error:nil])
            {
                if ([isDownloaded boolValue])
                {
                    return YES;
                }

                // Download the file.
                NSFileManager *fm = [NSFileManager defaultManager];
                if (![fm startDownloadingUbiquitousItemAtURL:file error:nil])
                {
                    return NO;
                }
                return YES;
            }
        }
    }

    // Return YES as long as an explicit download was not started.
    return YES;
}

// 点击textView其他区域，使键盘消失
- (void) touchesBegan : (NSSet *) touches withEvent : (UIEvent *) event
{
    [self.textView resignFirstResponder];
}

// 设置 Label 文本
- (void) setString : (NSString *) string
{
    self.label.text = string;
}

@end
